/**
 * Copyright (C)  2011  chen wei hua.
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 2.0
 * or any later version published by the Free Software Foundation;
 * with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU Free Documentation License".
 */
package com.javaeye.accounting.web;

import java.util.ArrayList;
import java.util.List;

import com.javaeye.accounting.dto.MaterialsMonthBatchDetail;
import com.javaeye.accounting.service.IMaterialsMonthBatchDetailService;
import com.javaeye.common.util.DateUtils;
import com.javaeye.common.util.NumberUtils;
import com.javaeye.store.dto.Materials;
import com.javaeye.store.dto.MaterialsCategory;
import com.javaeye.store.service.IMaterialsCategoryService;
import com.opensymphony.xwork2.ActionSupport;

public class MaterialsMonthBatchDetailAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = -197381205794811886L;
	
	
	/**
	 * 用于接受查询画面上选择日期的值
	 */
	private String queryDate;
	
	/**
	 * 用于修改初始化库存的在库品ID
	 */
	private int materialsId;
	
	private Materials materials;
	
	private List<Integer> detailIds;
	
	private List<Integer> skuDetailIds;
	
	private List<Double> prices;
	
	private List<Float> numbers;
	
	private List<Float> defectiveNumbers;
	
	private List<String> batchNos;
	
	/**
	 * 用于接受查询画面上选择分类的下拉框的值
	 */
	private int rootCategory;
	
	private String queryType;
	
	private double totleAmount;
	
	/**
	 * 用于生成查询画面上选择分类的下拉框的值
	 */
	private List<MaterialsCategory> rootCategoryList;
	
	/**
	 * 查询结果列表
	 */
	private List<MaterialsMonthBatchDetail> materialsBatchMonthDetails;
	
	private IMaterialsMonthBatchDetailService service;
	
	private IMaterialsCategoryService categoryService;
	
	public void setService(IMaterialsMonthBatchDetailService service) {
		this.service = service;
	}
	
	public void setCategoryService(IMaterialsCategoryService categoryService) {
		this.categoryService = categoryService;
	}

	public MaterialsMonthBatchDetailAction() {
		rootCategory = 26;
		queryDate = DateUtils.formatString(DateUtils.getBeforeMonthEndDate(), "yyyy-MM");
		queryType = "";
		materialsId = -1; 
	}
	
	public String queryMaterialsBatchMonthInfo() throws Exception {
		rootCategoryList = categoryService.getCategorys(0);
		materialsBatchMonthDetails = 
			service.getMaterialsBatchMonthInfo(queryDate, rootCategory, queryType);
		double value = 0;
		for (MaterialsMonthBatchDetail detail : materialsBatchMonthDetails) {
			value = detail.getNumber() + detail.getDefectiveNumber();
			value *= detail.getPrice();
			totleAmount += value;
		}
		totleAmount = NumberUtils.formatDouble(totleAmount);
		return SUCCESS;
	}
	
	/**
	 * 用于调整初始化库存，平时使用不到
	 * @throws Exception
	 */
	public String queryInitMaterialsBatchDetail() throws Exception {
		if (materialsId == -1) {
			return SUCCESS;
		}
		materialsBatchMonthDetails = service.getInitMaterialsBatchDetail(queryDate, materialsId);
		if (materialsBatchMonthDetails.size() > 0) {
			materials = materialsBatchMonthDetails.get(0).getSkuDetail().getMaterials();
		}
		return SUCCESS;
	}
	
	/**
	 * 用于调整初始化库存，平时使用不到
	 * @throws Exception
	 */
	public String saveInitMaterialsBatchDetail() throws Exception {
		if (detailIds == null || detailIds.size() == 0) {
			return SUCCESS;
		}
		List<MaterialsMonthBatchDetail> batchDetails = 
			new ArrayList<MaterialsMonthBatchDetail>();
		for (int i = 0; i < detailIds.size(); i++) {
			MaterialsMonthBatchDetail detail = new MaterialsMonthBatchDetail();
			detail.setBatchNo(batchNos.get(i));
			detail.setPrice(prices.get(i));
			detail.setNumber(numbers.get(i));
			detail.setDefectiveNumber(defectiveNumbers.get(i));
			batchDetails.add(detail);
		}
		service.saveInitMaterialsBatchDetail(queryDate, detailIds, skuDetailIds, batchDetails);
		this.addActionMessage("保存成功！");
		queryInitMaterialsBatchDetail();
		return SUCCESS;
	}

	public String getQueryDate() {
		return queryDate;
	}

	public void setQueryDate(String queryDate) {
		this.queryDate = queryDate;
	}

	public int getRootCategory() {
		return rootCategory;
	}

	public void setRootCategory(int rootCategory) {
		this.rootCategory = rootCategory;
	}

	public List<MaterialsCategory> getRootCategoryList() {
		return rootCategoryList;
	}

	public void setRootCategoryList(List<MaterialsCategory> rootCategoryList) {
		this.rootCategoryList = rootCategoryList;
	}

	public List<MaterialsMonthBatchDetail> getMaterialsBatchMonthDetails() {
		return materialsBatchMonthDetails;
	}

	public void setMaterialsBatchMonthDetails(
			List<MaterialsMonthBatchDetail> materialsBatchMonthDetails) {
		this.materialsBatchMonthDetails = materialsBatchMonthDetails;
	}

	public String getQueryType() {
		return queryType;
	}

	public void setQueryType(String queryType) {
		this.queryType = queryType;
	}

	public void setMaterialsId(int materialsId) {
		this.materialsId = materialsId;
	}

	public int getMaterialsId() {
		return materialsId;
	}

	public void setMaterials(Materials materials) {
		this.materials = materials;
	}

	public Materials getMaterials() {
		return materials;
	}

	public List<Integer> getDetailIds() {
		return detailIds;
	}

	public void setDetailIds(List<Integer> detailIds) {
		this.detailIds = detailIds;
	}

	public List<Integer> getSkuDetailIds() {
		return skuDetailIds;
	}

	public void setSkuDetailIds(List<Integer> skuDetailIds) {
		this.skuDetailIds = skuDetailIds;
	}

	public List<Double> getPrices() {
		return prices;
	}

	public void setPrices(List<Double> prices) {
		this.prices = prices;
	}

	public List<Float> getNumbers() {
		return numbers;
	}

	public void setNumbers(List<Float> numbers) {
		this.numbers = numbers;
	}

	public List<Float> getDefectiveNumbers() {
		return defectiveNumbers;
	}

	public void setDefectiveNumbers(List<Float> defectiveNumbers) {
		this.defectiveNumbers = defectiveNumbers;
	}

	public void setBatchNos(List<String> batchNos) {
		this.batchNos = batchNos;
	}

	public List<String> getBatchNos() {
		return batchNos;
	}

	public void setTotleAmount(double totleAmount) {
		this.totleAmount = totleAmount;
	}

	public double getTotleAmount() {
		return totleAmount;
	}
}
